<?php

namespace common\models\datas;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%customer_upgrade_rule}}".
 *
 * @property integer $id
 * @property integer $type_id
 * @property integer $grade_id
 * @property integer $site_id
 * @property string $money
 * @property integer $status
 * @property integer $admin_id
 * @property double $expiry_date
 * @property integer $created_at
 */
class CustomerUpgradeRule extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_upgrade_rule}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type_id', 'grade_id', 'site_id', 'status', 'admin_id', 'created_at'], 'integer'],
            [['money', 'expiry_date'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自增id'),
            'type_id' => Yii::t('common', '升级类型id'),
            'grade_id' => Yii::t('common', '等级id'),
            'site_id' => Yii::t('common', '分店id'),
            'money' => Yii::t('common', '充值/购买金额'),
            'status' => Yii::t('common', '状态 1 启用 2 禁用'),
            'admin_id' => Yii::t('common', '操作人id'),
            'expiry_date' => Yii::t('common', '有效期（单位：年）'),
            'created_at' => Yii::t('common', '下单时间'),
        ];
    }

    public function fields(){
        $fields = parent::fields();
        $fields['created_at'] = function(){
            return \Yii::$app->formatter->asDate($this->created_at);
        };
        $fields['status'] = function(){
            switch ($this->status){
                case 1:
                    return  Yii::t('common', '启用');
                case 2:
                    return Yii::t('common', '禁用');
            }
        };
        return $fields;
    }

    public function extrafields(){
        return [
            'gradename'=>function(){
                 return isset($this->grade->name)?$this->grade->name:'';
            },
            'operate' => function(){
                return '';
            }
        ];
    }

    public function getGrade(){
        return $this->hasOne(Customergrade::className(),['id'=>'grade_id']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],

        ]);

    }
}
